package com.example.uchain.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.example.uchain.demo.Member;
import com.example.uchain.demo.PageBean;
import com.example.uchain.demo.Result;
import com.example.uchain.demo.Trade;
import com.example.uchain.demo.dto.TradeCountDTO;
import com.example.uchain.mapper.MemberMapper;
import com.example.uchain.mapper.TradeMapper;
import com.example.uchain.service.TradeService;
import com.example.uchain.utils.TradeCode;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class TradeServicelmpl implements TradeService {

    @Autowired
    private TradeMapper tradeMapper;

//    @Autowired
//    WeBASEUtils weBASEUtils;

    @Autowired
    private MemberMapper memberMapper;

//    @Transactional(rollbackFor =Exception.class)//出现所有异常都将事务回滚  spring事务管理
    //分页查询 未购买
    @Override
    public PageBean page(
            Integer Page,
            Integer pageSize,
            String ownerName,
            String workName,
            Integer CRId,
            String workType,
            String authorization,
            String blockAddress,
            Integer code) {

        //设置分页参数
        PageHelper.startPage(Page, pageSize);

        //执行查询
        List<Trade> tradeList = tradeMapper.findAll(ownerName,workName,CRId,workType,authorization,blockAddress,code);

        // 获取分页信息
        Page<Trade> p = (Page<Trade>)  tradeList;

        //封装pageBean对象
        PageBean pageBean1 = new PageBean(p.getTotal(), p.getResult());
        return pageBean1;
    }

    //分页查询 已收藏
    @Override
    public PageBean page1(
            Integer Page,
            Integer pageSize,
            String ownerName,
            String workName,
            Integer CRId,
            String workType,
            String authorization,
            String blockAddress,
            Integer code) {

        //设置分页参数
        PageHelper.startPage(Page, pageSize);

        //执行查询
        List<Trade> tradeList = tradeMapper.findAll2(ownerName,workName,CRId,workType,authorization,blockAddress,code);

        // 获取分页信息
        Page<Trade> p = (Page<Trade>)  tradeList;

        //封装pageBean对象
        PageBean pageBean1 = new PageBean(p.getTotal(), p.getResult());
        return pageBean1;
    }

    //分页查询 已收藏 未购买
    @Override
    public PageBean page2(
            Integer Page,
            Integer pageSize,
            String ownerName,
            String workName,
            Integer CRId,
            String workType,
            String authorization,
            String blockAddress,
            Integer code,
            Integer uid) {

        //设置分页参数
        PageHelper.startPage(Page, pageSize);

        //执行查询
        List<Trade> tradeList = tradeMapper.findAll3(ownerName,workName,CRId,workType,authorization,blockAddress,code,uid);

        // 获取分页信息
        Page<Trade> p = (Page<Trade>)  tradeList;

        //封装pageBean对象
        PageBean pageBean1 = new PageBean(p.getTotal(), p.getResult());
        return pageBean1;
    }

    //分页查询 已收藏 已购买
    @Override
    public PageBean page3(
            Integer Page,
            Integer pageSize,
            String ownerName,
            String workName,
            Integer CRId,
            String workType,
            String authorization,
            String blockAddress,
            Integer code,
            Integer uid) {

        //设置分页参数
        PageHelper.startPage(Page, pageSize);

        //执行查询
        List<Trade> tradeList = tradeMapper.findAll4(ownerName,workName,CRId,workType,authorization,blockAddress,code,uid);

        // 获取分页信息
        Page<Trade> p = (Page<Trade>)  tradeList;

        //封装pageBean对象
        PageBean pageBean1 = new PageBean(p.getTotal(), p.getResult());
        return pageBean1;
    }

    //分页查询  已购买
    @Override
    public PageBean page4(
            Integer Page,
            Integer pageSize,
            String ownerName,
            String workName,
            Integer CRId,
            String workType,
            String authorization,
            String blockAddress,
            Integer code,
            Integer uid) {

        //设置分页参数
        PageHelper.startPage(Page, pageSize);

        //执行查询
        List<Trade> tradeList = tradeMapper.findAll5(ownerName,workName,CRId,workType,authorization,blockAddress,code,uid);

        // 获取分页信息
        Page<Trade> p = (Page<Trade>)  tradeList;

        //封装pageBean对象
        PageBean pageBean1 = new PageBean(p.getTotal(), p.getResult());
        return pageBean1;
    }


    //添加
    @Override
    public void AddTrade(Trade trade) {
        tradeMapper.AddTrade(trade);
    }

    //删除
    @Override
    public Result deleteTradeById(Integer TId) {
        if (tradeMapper.findAllByTid(TId) == null){
            log.info("该交易信息不存在");
            return Result.error("该交易信息不存在");
        }else {
            tradeMapper.deleteTradeById(TId);
            return Result.success();
        }
    }

    //修改
    @Override
    public void updateById(Trade trade) {
        tradeMapper.updateById(trade);
    }

    //修改购买状态
    @Override
    public void updateById1(Trade trade) {
        log.info("修改购买状态{}",trade);
        trade.setTreadTime(LocalDateTime.now());
        tradeMapper.updateById1(trade);
    }

    //修改收藏状态
    @Override
    public Result updateById2(Trade trade) {
        tradeMapper.updateById2(trade);
        return Result.success(tradeMapper.findAll1(trade.getCRId())); // 将修改后的记录作为返回值返回
    }
    //修改展示状态
    @Override
    public Result updateById3(Trade trade) {
        tradeMapper.updateById3(trade);
        return Result.success(tradeMapper.findAll1(trade.getCRId())); // 将修改后的记录作为返回值返回
    }

    @Override
    public List<Trade> findAllPeople() {
        return tradeMapper.findAllPeople();
    }

    @Override
    public  List<Trade> tradeFindByUid(Integer uid) {
       return tradeMapper.tradeFindByUid(uid);
    }



    //根据id查找数据
    @Override
    public List<Trade> findAll1(Integer CRId) {
        return  tradeMapper.findAll1(CRId);
    }


    @Override
    public TradeCountDTO tradeCount() {
        List<Map<String, Object>> newOrderCountsByDay = tradeMapper.newTradeCountByDay();//每日新增订单数量

        List<Map<String, Object>> dailyOrderCounts = getCumulativeCounts(newOrderCountsByDay); //每日总订单数量

        TradeCountDTO result = new TradeCountDTO();
        result.setNewTotalCount(newOrderCountsByDay);
        result.setTotalTradeCount(dailyOrderCounts);

        return result;
    }

    /**
     * 订单排名top10
     * @return
     */
    @Override
    public TradeCountDTO OrderAndCount() {
        List<Map<String, Object>> OrderAndCount = tradeMapper.OrderAndCount();

        TradeCountDTO result = new TradeCountDTO();
        result.setOrderAndCount(OrderAndCount);

        // 遍历并添加用户名
        for (Map<String, Object> entry : OrderAndCount) {
            Long uidLong = (Long) entry.get("uid");
            Integer uid = uidLong.intValue();
            Member member = memberMapper.findAll1(uid);
            if (member != null) {
                entry.put("username", member.getUserName()); // 假设用户名字段为username
            }
        }
        return result;
    }

    @Override
    public List<Trade> getAllTradings() {
        return tradeMapper.findAll(
                null,  // ownerName
                null,  // workName
                null,  // CRId
                null,  // workType
                null,  // authorization
                null,  // blockAddress
                null   // code
        );
    }


    @NotNull
    private static List<Map<String, Object>> getCumulativeCounts(List<Map<String, Object>> dailyAuthorCounts) {
        List<Map<String, Object>> resultWithCumulativeCounts = new ArrayList<>();
        long runningTotal = 0L; // 初始化累加计数器

        for (Map<String, Object> dayCount : dailyAuthorCounts) {
            // 创建一个新的Map以包含原有数据并避免修改原列表
            Map<String, Object> updatedCount = new HashMap<>(dayCount);

            // 获取当前条目的"count"值，如果不存在则默认为0
            Long currentCount = (Long) updatedCount.getOrDefault("count", 0L);

            // 更新累加计数器
            runningTotal += currentCount;

            // 在新Map中添加累计计数的字段，例如命名为"cumlative_total"
            updatedCount.put("cumulative_total", runningTotal);

            // 将更新后的条目（含原始计数和累计计数）加入到新列表中
            resultWithCumulativeCounts.add(updatedCount);
        }
        return resultWithCumulativeCounts;
    }



    //交易信息上链
    @Override
    public Result AddChain1(Trade trade) {

        if (trade.getCRId() == null ||
                StrUtil.isEmpty(trade.getWorkName()) ||
                StrUtil.isEmpty(trade.getWorkType())  ||
                StrUtil.isEmpty(trade.getBlockAddress()) ||
                StrUtil.isEmpty(trade.getAuthorization())||
                trade.getPrice() == null
        ) {
            return Result.error(String.valueOf(Result.PARAM_EMPTY));
        }

        TradeCode myContract = new TradeCode();
        String  _result= String.valueOf(myContract.addTrade(trade.getCRId(),trade.getWorkName(),trade.getWorkType(),trade.getBlockAddress(),trade.getAuthorization(), String.valueOf(trade.getPrice())));
        log.info("_result:{}",_result);
        if ((_result.equals("null"))){
            return Result.error(String.valueOf(Result.CONTRACT_ERROR));
        }
        else {
            JSONObject _resultJson = JSONUtil.parseObj(_result);
            if (!_resultJson.containsKey("statusOK") || !_resultJson.getBool("statusOK")){
                return Result.error(String.valueOf(Result.CONTRACT_ERROR));
            }else{
                // 创建一个 Map 对象，将数据放入其中
                Map<String, Object> data = new HashMap<>();

                String transactionHash = _resultJson.getStr("transactionHash");
                String transactionIndex = _resultJson.getStr("gasUsed");
                String blockNumber = _resultJson.getStr("blockNumber");
                String blockHash = _resultJson.getStr("blockHash");
                String from = _resultJson.getStr("from");
                String to = _resultJson.getStr("to");
                String message = _resultJson.getStr("message");
                String statusOK = _resultJson.getStr("statusOK");

                data.put("transactionHash", transactionHash);
                data.put("transactionIndex", transactionIndex);
                data.put("blockNumber", blockNumber);
                data.put("blockHash", blockHash);
                data.put("from", from);
                data.put("to", to);
                data.put("message", message);
                data.put("statusOK", statusOK);

                // 将 Map 对象转换为 JSON 字符串
                String jsonData = JSONUtil.toJsonStr(data);

                log.info("jsonData: {}", jsonData);

                // 将 JSON 字符串放入一个对象中并返回给前端
                Map<String, Object> result = new HashMap<>();
                result.put("code", 200);
                result.put("msg", "success");
                result.put("data", jsonData);

                return Result.success(result);
            }
        }
    }

}
